namespace Resonator.Tests.Services
{
	using System.Collections.Generic;
	using System.Globalization;
	using System.IO;
	using NUnit.Framework;
	using Resonator.Core.Services;
	using Resonator.Domain.Models;
	using Resonator.Domain.Services;

	[TestFixture]
	[Category("Exporter")]
	public class ResxResourceExporterTester
	{
		private IResourceExporter _exporter;
		private IResourceParser _parser;

		[SetUp]
		public void SetUp()
		{
			_exporter = new ResxResourceExporter();
			_parser = new ResxResourceParser();
		}

		[Test]
		public void Extension_Is_Resx()
		{
			Assert.AreEqual( ".resx", _exporter.Extension );
		}

		[Test]
		public void Writes_Phrase_As_Node_Value()
		{
			List<Resource> exportedResources = new List<Resource>();
			exportedResources.Add( Resource.Create( "Yes", "btnYes.Text" ) );

			MemoryStream stream = new MemoryStream();
			_exporter.Export( stream, exportedResources );

			stream.Seek( 0, SeekOrigin.Begin );

			IList<Resource> parsedResources = _parser.Parse( new ResourceStream( "Test.resx", stream ) );

			Assert.AreEqual( 1, parsedResources.Count );
			Assert.AreEqual( "Yes", parsedResources[0].Phrase );
			Assert.AreEqual( "Test.btnYes.Text", parsedResources[0].Name );
		}

		[Test]
		public void Writes_Phrase_Of_Desired_Culture()
		{
			List<Resource> exportedResources = new List<Resource>();
			exportedResources.Add( Resource.Create( "Yes", "btnYes.Text" ).AddTranslation( "es-ES", "Si" ) );

			MemoryStream stream = new MemoryStream();
			_exporter.Export( stream, exportedResources, new CultureInfo( "es-ES" ) );
			stream.Seek( 0, SeekOrigin.Begin );

			IList<Resource> parsedResources = _parser.Parse( new ResourceStream( "Test.es-ES.resx", stream ) );

			Assert.AreEqual( 1, parsedResources.Count );
			Assert.AreEqual( "Si", parsedResources[0].Phrase );
			Assert.AreEqual( "Test.btnYes.Text", parsedResources[0].Name );
		}

		[Test]
		public void Writes_Context_As_Node_Comment()
		{
			List<Resource> exportedResources = new List<Resource>();
			exportedResources.Add( Resource.Create( "Yes", "btnYes.Text", "Button that the user uses to signal the affirmative" ) );

			MemoryStream stream = new MemoryStream();
			_exporter.Export( stream, exportedResources );
			stream.Seek( 0, SeekOrigin.Begin );

			IList<Resource> parsedResources = _parser.Parse( new ResourceStream( "Test.resx", stream ) );

			Assert.AreEqual( 1, parsedResources.Count );
			Assert.AreEqual( "Button that the user uses to signal the affirmative", parsedResources[0].Context );
		}
	}
}